<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/4
 * Time: 21:36
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\UserFormId;
use Carbon\Carbon;
use App\Http\Logics\BaseLogic;
class UserFormIdLogic extends BaseLogic
{
    /**
     * 创建用户的form_id
     * @param $data
     * @return mixed
     */
    public static function createUserFormId($data)
    {
        $user_form_id = new UserFormId();
        $user_form_id->user_id = $data['user_id'];
        $user_form_id->form_id = $data['form_id'];
        $user_form_id->expired_at = strtotime("+7 day");
        if (!$user_form_id->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 获取一个form_id
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public static function getUserFormId($data)
    {
        $user_form_id = UserFormId::where('user_id', $data['user_id'])->where('expired_at', '>', Carbon::now())->orderBy('id', 'asc')->first();
        if (empty($user_form_id)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $form_id = $user_form_id->form_id;
        if (!$user_form_id->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS, $form_id);
    }
}